package com.ruitu.health_2.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.bumptech.glide.Glide;
import com.hjq.permissions.Permission;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.PhoneUtils;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.ArtManModel;
import com.ruitu.health_2.mvp.presenter.ArtManPresenter;
import com.ruitu.health_2.ui.fragment.ArticleListFragment;
import com.ruitu.health_2.ui.fragment.ProductListFragment;
import com.ruitu.health_2.ui.fragment.VideoListFragment;
import com.ruitu.router_module.bean.article.Article;
import com.ruitu.router_module.bean.product.ArtMan;
import com.ruitu.router_module.bean.product.ArtManDetail;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.bean.product.Video;
import com.ruitu.router_module.support.view.MeasuredNoScrollViewPager;
import com.ruitu.router_module.support.view.MyNestedScrollView;
import com.ruitu.router_module.util.PermissionUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

// 艺人详情
public class ArtManDetailActivity extends BaseActivity<ArtManPresenter.ArtManDetailP, ArtManModel.ArtManDetailM> {
    private MyNestedScrollView sv_content;
    private MeasuredNoScrollViewPager vp_pro;
    private XTabLayout tab, tab_2;
    private ImageView iv_header;// 头像
    private TextView tv_name, tv_tag;// 姓名,标签
    private RelativeLayout rl_call, rl_online; //电话,在线
    private RelativeLayout rl_header, rl_pro, rl_tab;// 艺人头部,tab,tab2

    private ArtMan artMan;// 艺人
    private List<ProHome> proList = new ArrayList<>();// 艺人商品列表
    private List<Article> articleList = new ArrayList<>();// 艺人文章列表
    private List<Video> videoList = new ArrayList<>();// 艺人视频列表
    public static final String[] HOME_MODELS = {"- -", "- -", "- -"};//
    public static final String[] HOME_MODELS_2 = {"商品", "视频", "文章"};//
    private VpAdapter vpAdapter;

    private String id = "";// 传过来的艺人id
    private String name = "艺人详情";// 传过来的艺人name

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_art_man_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sv_content = findViewById(R.id.sv_content);
        vp_pro = findViewById(R.id.vp_pro);
        tab = findViewById(R.id.tab);
        tab_2 = findViewById(R.id.tab_2);
        iv_header = findViewById(R.id.iv_header);
        tv_name = findViewById(R.id.tv_name);
        tv_tag = findViewById(R.id.tv_tag);
        rl_call = findViewById(R.id.rl_call);
        rl_online = findViewById(R.id.rl_online);
        rl_header = findViewById(R.id.rl_header);
        rl_pro = findViewById(R.id.rl_pro);
        rl_tab = findViewById(R.id.rl_tab);

        setListeners();

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            id = bundle.getString("id");
            name = bundle.getString("name");
        }

        vp_pro.setOffscreenPageLimit(99);
        vp_pro.setScanScroll(false);
        for (int i = 0; i < 3; i++) {
            tab.addTab(tab.newTab().setText(HOME_MODELS[i]), i);
            tab_2.addTab(tab_2.newTab().setText(HOME_MODELS[i]), i);
        }
        vpAdapter = new VpAdapter(getSupportFragmentManager());

        setHeadTitle(name);

        reqData();
        showProgress();
    }

    private void setListeners() {
        setOnClickListener(rl_call, rl_online);
        sv_content.setScrollViewListener(new MyNestedScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(MyNestedScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (y >= tabY) {
                    rl_tab.setVisibility(View.VISIBLE);
                } else {
                    rl_tab.setVisibility(View.GONE);
                }
            }
        });
//        tab.addOnTabSelectedListener(new OnTabSelectedListenerImpl() {
//            @Override
//            public void onTabSelected(XTabLayout.Tab tab) {
//                tab_2.getTabAt(tab.getPosition()).select();
//            }
//        });
//        tab_2.addOnTabSelectedListener(new OnTabSelectedListenerImpl() {
//            @Override
//            public void onTabSelected(XTabLayout.Tab tab) {
//                Home2Fragment.this.tab.getTabAt(tab.getPosition()).select();
//            }
//        });
    }

    private void reqData() {
        p.reqArtManDetail(id);
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {
            ArtManDetail detail = (ArtManDetail) data;
            artMan = detail.getUmsArtMan();
            articleList = detail.getArticleList();
            videoList = detail.getVideoList();
            proList = detail.getProductsList();

            Glide.with(Arad.app).load(artMan.getAvaterUrl()).apply(Arad.opts_circle).into(iv_header);
            tv_name.setText(artMan.getName());
            tv_tag.setText(TextUtils.isEmpty(artMan.getIntro()) ? "暂无标签" : artMan.getIntro());

            vp_pro.setAdapter(vpAdapter);
            tab.setupWithViewPager(vp_pro);
            tab_2.setupWithViewPager(vp_pro);
        }

        getTabY();
    }

    private int tabY = 0;// tab的y坐标

    private void getTabY() {
//        int[] position = new int[2];
//        tab.getLocationInWindow(position);
//        tabY = position[1];
        tabY = rl_header.getMeasuredHeight() + SizeUtils.dp2px(30);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == rl_call) {
            final String phoneNum = artMan.getPhone();
            PermissionUtil.handleSinglePermission(this, Permission.CALL_PHONE, "拨打电话", new PermissionUtil.PermissionListener() {
                @Override
                public void onPermissionOk(boolean isOk) {
                    if (isOk) {
                        if (!TextUtils.isEmpty(phoneNum)) {
                            try {
                                PhoneUtils.dial(phoneNum);
                            } catch (Exception e) {
                                e.printStackTrace();
                                ToastUtils.showShortSafe("无权限或设备不支持拨号");
                            }
                        } else {
                            ToastUtils.showShortSafe("该艺人电话未公开");
                        }
                    }
                }

                @Override
                public void onOpenPermissionPage(boolean isOpen) {

                }
            });
        }
        if (v == rl_online) {
            ToastUtils.showShortSafe("在线咨询");
        }
    }

    private class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return ProductListFragment.newInstance(proList);
            } else if (position == 1) {
                return VideoListFragment.newInstance(videoList);
            } else if (position == 2) {
                return ArticleListFragment.newInstance(articleList);
            }
            return ProductListFragment.newInstance(proList);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return HOME_MODELS_2[position];
        }
    }
}
